package com.lanou3g.demo.dao;

import com.lanou3g.demo.db.DBConnector;
import com.lanou3g.demo.db.EntityHandler;
import com.lanou3g.demo.db.EntityListHandler;
import com.lanou3g.demo.entity.BookEntity;
import com.lanou3g.demo.entity.CategoryEntity;
import com.lanou3g.demo.entity.OrderEntity;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BookDao {

    //查看所有图书的集合
    public static List<BookEntity> BookAll () throws SQLException {
        Connection conn= DBConnector.getInstance().getConnection();
        QueryRunner runner=new QueryRunner();
        String sql="select * from book";
        List<BookEntity> bookEntityList = runner.query(conn, sql, new EntityListHandler<>(BookEntity.class));
        return bookEntityList;
    }


    //通过bid来查找图片信息
    public static BookEntity bookEntity(String bid) throws SQLException {
        Connection conn = DBConnector.getInstance().getConnection();
        QueryRunner runner = new QueryRunner();
        String sql = "select * from book where bid = ?";
        BookEntity bookEntity = runner.query(conn, sql, new EntityHandler<>(BookEntity.class), bid);
        return bookEntity;
    }


    //添加图书
    public static void addbook (String bid,String bname,float price,String authod,String image,String cid) throws SQLException {
        Connection conn=DBConnector.getInstance().getConnection();
        QueryRunner runner=new QueryRunner();
        String sql="insert into book value(?,?,,?,?,?,?)";
        runner.update(conn,sql,cid,bid,bname,price,authod,image,cid);
    }

    //根据bid删除图书
    public static void deleteBook (String bid) throws SQLException {
        Connection conn=DBConnector.getInstance().getConnection();
        QueryRunner runner=new QueryRunner();
        String sql="delete from book where bid = ?";
        runner.update(conn,sql,bid);
    }

    //根据bid修改图书
    public static void updateBook (String bid,String bname,float price,String authod,String image,String cid) throws SQLException {
        Connection conn=DBConnector.getInstance().getConnection();
        QueryRunner runner=new QueryRunner();
        String sql="update category set bname = ?,price = ?,author = ?,image = ? where bid = ?";
        runner.update(conn,sql,bname,price,authod,image,cid,bid);
    }
}
